package com.rxt.thermopro.viewmodel


import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.app.common.ex.logD
import com.app.common.viewmodel.BaseViewModel
import com.app.common.viewmodel.EventLiveData
import com.rxt.ble.BleManager
import com.rxt.ble.callback.BleWriteCallback
import com.rxt.ble.exception.BleException
import com.rxt.ble.utils.HexUtil
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProfileBean
import com.rxt.thermopro.bean.TempBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class TempGraphPageViewModel : BaseViewModel() {
    val profileData: MutableLiveData<ProfileBean> = MutableLiveData()


    fun getProfile(position: Int, address: String) {
        val profile = when (position) {
            0 -> {
                LocalDataDao.INSTANCE.getProfile1(address)
            }
            1 -> {
                LocalDataDao.INSTANCE.getProfile2(address)
            }
            2 -> {
                LocalDataDao.INSTANCE.getProfile3(address)
            }
            3 -> {
                LocalDataDao.INSTANCE.getProfile4(address)
            }
            else -> LocalDataDao.INSTANCE.getProfile1(address)
        }
        profileData.value = profile
    }

    fun refreshHistoryTemp(address: String, probeIndex: Int) {
        isWorking = true
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                while (isWorking) {
                    withContext(Dispatchers.Main) {
                        getHistoryTemp(address, probeIndex)
                    }
                    delay(60 * 1000)
                }
            }
        }
    }

    private var isWorking = true

    /**
     * 获取历史数据
     */
    fun getHistoryTemp(address: String, probeIndex: Int) {
        val msg = "020$probeIndex"
        println("getHistoryTemp msg====$msg")
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                TP960ViewModel.BLUETOOTH_SERVICE_UUID_960,
                TP960ViewModel.BLUETOOTH_WRITE_UUID_960,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 setProfile  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }

    override fun onCleared() {
        isWorking = false
        super.onCleared()
    }

}